AddCSLuaFile( "cl_init.lua" )
AddCSLuaFile( "shared.lua" )
util.PrecacheSound( "apc_engine_start" )
util.PrecacheSound( "apc_engine_stop" )
util.PrecacheSound( "ambient/machines/thumper_startup1.wav" )
util.PrecacheSound( "Other/missilespawn.wav" )
util.PrecacheSound( "common/warning.wav" )

include('shared.lua')

local Ground = 1 + 0 + 2 + 8 + 32

function ENT:Initialize()
	self.Entity:SetModel( "models//props_wasteland/laundry_dryer002.mdl" )
	self.Entity:PhysicsInit( SOLID_VPHYSICS )
	self.Entity:SetMoveType( MOVETYPE_VPHYSICS )
	self.Entity:SetSolid( SOLID_VPHYSICS )
	self.Entity:SetMaterial("models/props_combine/combinethumper002");
	self.Inputs = Wire_CreateInputs(self.Entity, { "Dumb-Missile", "OrdinanceRack", "HellfireWarhead", "KrakAPWarhead", "ClusterWarhead", "ChemicalWarhead", "WPWarhead", "ShockwaveWarhead", "PlasmaBlastWarhead", "NuclearWarhead", "BiohazardWarhead" } )
	local phys = self.Entity:GetPhysicsObject()
	self:NextThink(CurTime() + 1)
	if (phys:IsValid()) then
		phys:Wake()
	end
	
	reqCool = 0
	reqHeavy = 0
	reqEnergy = 0
	self.val1 = 0
	self.val2 = 0
	self.val3 = 0
	LS_RegisterEnt(self.Entity)
	RD_AddResource(self.Entity, "coolant", 0)
	RD_AddResource(self.Entity, "heavy water", 0)
	RD_AddResource(self.Entity, "energy", 0)
	self.Outputs = Wire_CreateOutputs(self.Entity, { "Working", "BuildTime", "ReqCoolant", "ReqHeavyWater", "ReqEnergy" }) 
	
end

function ENT:SpawnFunction( ply, tr )

	if ( !tr.Hit ) then return end

	local SpawnPos = tr.HitPos + tr.HitNormal * 36

	local ent = ents.Create( "warhead_factory" )
	ent:SetPos( SpawnPos )
	ent:Spawn()
	ent:Activate()
	return ent
end

function ENT:Use()

end

function ENT:TriggerInput(iname, value)
	self.Entity:EmitSound( "apc_engine_start" )
	if (iname == "Dumb-Missile") then
		if (value > 0) then
			if (self.Working == false) then
				reqCool = 100
				reqHeavy = 0
				reqEnergy = 0
				if (self.val1 >= 100) then
					self.CurProj = "forocket"
					RD_ConsumeResource(self, "coolant", 200)
					self.NTog = CurTime() + 2
					self.Working = true
					self.PIni = true
					self.PWHType = 0
					self.PWarhead = nil
					self.NWR = 255
					self.NWG = 255
					self.NWB = 255
					self.NWA = 255
				end
			end
		end
	
	elseif (iname == "BL-HEShell") then
		if (value > 0) then
			if (self.Working == false) then
				if (self.val1 >= 100) then
					self.CurProj = "blshell"
					RD_ConsumeResource(self, "coolant", 100)
					self.NTog = CurTime() + 3
					self.Working = true
					self.PIni = true
					self.PWHType = 1
					self.PWarhead = nil
					self.NWR = 255
					self.NWG = 155
					self.NWB = 155
					self.NWA = 255
				end
			end
		end	

	elseif (iname == "OR-ShockwaveShell") then
		if (value > 0) then
			if (self.Working == false) then
				if (self.val1 >= 700) then
					self.CurProj = "bigshell"
					RD_ConsumeResource(self, "coolant", 700)
					self.NTog = CurTime() + 2
					self.Working = true
					self.PIni = true
					self.PWHType = -1
					self.PWarhead = nil
					self.NWR = 255
					self.NWG = 255
					self.NWB = 255
					self.NWA = 255
				end
			end
		end

	elseif (iname == "OR-LightShell") then
		if (value > 0) then
			if (self.Working == false) then
				if (self.val1 >= 100) then
					self.CurProj = "lightshell"
					RD_ConsumeResource(self, "coolant", 100)
					self.NTog = CurTime() + 2
					self.Working = true
					self.PIni = true
					self.PWHType = -1
					self.PWarhead = nil
					self.NWR = 255
					self.NWG = 255
					self.NWB = 255
					self.NWA = 255
				end
			end
		end	

	elseif (iname == "OR-MediumShell") then
		if (value > 0) then
			if (self.Working == false) then
				if (self.val1 >= 300) then
					self.CurProj = "medshell"
					RD_ConsumeResource(self, "coolant", 300)
					self.NTog = CurTime() + 4
					self.Working = true
					self.PIni = true
					self.PWHType = -1
					self.PWarhead = nil
					self.NWR = 255
					self.NWG = 255
					self.NWB = 255
					self.NWA = 255
				end
			end
		end	

	elseif (iname == "OR-UnguidedMissile") then
		if (value > 0) then
			if (self.Working == false) then
				if (self.val1 >= 400) then
					self.CurProj = "ugshell"
					RD_ConsumeResource(self, "coolant", 400)
					self.NTog = CurTime() + 3
					self.Working = true
					self.PIni = true
					self.PWHType = -1
					self.PWarhead = ""
					self.NWR = 255
					self.NWG = 255
					self.NWB = 255
					self.NWA = 255
				end
			end
		end	
	
		
	elseif (iname == "BL-CustomShell") then
		if (value > 0) then
			if (self.Working == false) then
				if (self.val1 >= 50) then
					self.CurProj = "blshell"
					RD_ConsumeResource(self, "coolant", 50)
					self.NTog = CurTime() + 1
					self.Working = true
					self.PIni = true
					self.PWHType = 2
					self.PWarhead = "exp_head"
					self.NWR = 55
					self.NWG = 55
					self.NWB = 55
					self.NWA = 255
				end
			end
		end

	elseif (iname == "MineCluster") then
		if (value > 0) then
			if (self.Working == false) then
				if (self.val1 >= 200 && self.val3 >= 300) then
					self.CurProj = "minecluster"
					RD_ConsumeResource(self, "coolant", 200)
					RD_ConsumeResource(self, "energy", 300)
					self.NTog = CurTime() + 1
					self.Working = true
					self.PIni = true
					self.PWHType = -1
					self.PWarhead = ""
					self.NWR = 200
					self.NWG = 200
					self.NWB = 200
					self.NWA = 255
				end
			end
		end
	
	elseif (iname == "HellfireWarhead") then
		if (value > 0) then
			if (self.Working == false) then
				reqCool = 1000
				reqHeavy = 5
				reqEnergy = 0
				if (self.val1 >= 1000 && self.val2 >= 5) then
					self.CurProj = "dwarhead"
					RD_ConsumeResource(self, "coolant", 1000)
					RD_ConsumeResource(self, "heavy water", 5)
					self.NTog = CurTime() + 10
					self.Working = true
					self.PIni = false
					self.PWHType = -1
					self.PWarhead = "hellfire_head"
					self.NWR = 255
					self.NWG = 55
					self.NWB = 55
					self.NWA = 255
				end
			end
		end
		
	elseif (iname == "KrakAPWarhead") then
		if (value > 0) then
			if (self.Working == false) then
				reqCool = 1500
				reqHeavy = 5
				reqEnergy = 0
				if (self.val1 >= 1500 && self.val2 >= 5) then
					self.CurProj = "dwarhead"
					RD_ConsumeResource(self, "coolant", 1500)
					RD_ConsumeResource(self, "heavy water", 5)
					self.NTog = CurTime() + 12
					self.Working = true
					self.PIni = false
					self.PWHType = -1
					self.PWarhead = "krak_head"
					self.NWR = 155
					self.NWG = 55
					self.NWB = 55
					self.NWA = 255
				end
			end
		end
		
	elseif (iname == "ClusterWarhead") then
		if (value > 0) then
			if (self.Working == false) then
				reqCool = 1000
				reqHeavy = 5
				reqEnergy = 0
				if (self.val1 >= 1000 && self.val2 >= 5) then
					self.CurProj = "dwarhead"
					RD_ConsumeResource(self, "coolant", 1000)
					RD_ConsumeResource(self, "heavy water", 5)
					self.NTog = CurTime() + 6
					self.Working = true
					self.PIni = false
					self.PWHType = -1
					self.PWarhead = "cluster_head"
					self.NWR = 55
					self.NWG = 55
					self.NWB = 55
					self.NWA = 255
				end
			end
		end
		
	elseif (iname == "ChemicalWarhead") then
		if (value > 0) then
			if (self.Working == false) then
				reqCool = 2000
				reqHeavy = 10
				reqEnergy = 0
				if (self.val1 >= 2000 && self.val2 >= 10) then
					self.CurProj = "dwarhead"
					RD_ConsumeResource(self, "coolant", 2000)
					RD_ConsumeResource(self, "heavy water", 10)
					self.NTog = CurTime() + 19
					self.Working = true
					self.PIni = false
					self.PWHType = -1
					self.PWarhead = "tibchem_head"
					self.NWR = 0
					self.NWG = 255
					self.NWB = 0
					self.NWA = 255
				end
			end
		end
	
	elseif (iname == "NuclearWarhead") then
		if (value > 0) then
			if (self.Working == false) then
				reqCool = 10000
				reqHeavy = 2000
				reqEnergy = 200000
				if (self.val1 >= 10000 && self.val2 >= 2000 && self.val3 >= 200000) then
					self.CurProj = "dwarhead"
					RD_ConsumeResource(self, "coolant", 10000)
					RD_ConsumeResource(self, "energy", 200000)
					RD_ConsumeResource(self, "heavy water", 2000)
					self.NTog = CurTime() + 180
					self.Working = true
					self.PIni = false
					self.PWHType = -1
					self.PWarhead = "tetanuke_head"
					self.NWR = 0
					self.NWG = 170
					self.NWB = 170
					self.NWA = 255
				end
			end
		end
		
	elseif (iname == "BiohazardWarhead") then
		if (value > 0) then
			if (self.Working == false) then
				reqCool = 50000
				reqHeavy = 50000
				reqEnergy = 50000
				if (self.val1 >= 50000 && self.val2 >= 50000 && self.val3 >= 50000) then
					self.CurProj = "dwarhead"
					RD_ConsumeResource(self, "coolant", 50000)
					RD_ConsumeResource(self, "energy", 50000)
					RD_ConsumeResource(self, "heavy water", 50000)
					self.NTog = CurTime() + 5
					self.Working = true
					self.PIni = false
					self.PWHType = -1
					self.PWarhead = "infection_head"
					self.NWR = 255
					self.NWG = 50
					self.NWB = 0
					self.NWA = 255
				end
			end
		end
		
	elseif (iname == "WPWarhead") then
		if (value > 0) then
			if (self.Working == false) then
				reqCool = 1000
				reqHeavy = 3
				reqEnergy = 0
				if (self.val1 >= 1000 && self.val2 >= 3) then
					self.CurProj = "dwarhead"
					RD_ConsumeResource(self, "coolant", 1000)
					RD_ConsumeResource(self, "heavy water", 3)
					self.NTog = CurTime() + 10
					self.Working = true
					self.PIni = false
					self.PWHType = -1
					self.PWarhead = "wp_head"
					self.NWR = 255
					self.NWG = 0
					self.NWB = 0
					self.NWA = 255
				end
			end
		end
	
	elseif (iname == "ShockwaveWarhead") then
		if (value > 0) then
			if (self.Working == false) then
				reqCool = 1000
				reqHeavy = 8
				reqEnergy = 0
				if (self.val1 >= 1000 && self.val2 >= 8) then
					self.CurProj = "dwarhead"
					RD_ConsumeResource(self, "coolant", 1000)
					RD_ConsumeResource(self, "heavy water", 8)
					self.NTog = CurTime() + 30
					self.Working = true
					self.PIni = false
					self.PWHType = -1
					self.PWarhead = "swave_head"
					self.NWR = 200
					self.NWG = 170
					self.NWB = 140
					self.NWA = 255
				end
			end
		end

	elseif (iname == "PlasmaBlastWarhead") then
		if (value > 0) then
			if (self.Working == false) then
				reqCool = 6000
				reqHeavy = 20
				reqEnergy = 10000
				if (self.val1 >= 6000 && self.val2 >= 20  && self.val3 >= 10000) then
					self.CurProj = "dwarhead"
					RD_ConsumeResource(self, "coolant", 6000)
					RD_ConsumeResource(self, "energy", 10000)
					RD_ConsumeResource(self, "heavy water", 20)
					self.NTog = CurTime() + 60
					self.Working = true
					self.PIni = false
					self.PWHType = -1
					self.PWarhead = "plasbomb_head"
					self.NWR = 80
					self.NWG = 180
					self.NWB = 255
					self.NWA = 255
				end
			end
		end

	elseif (iname == "OrdinanceRack") then
		if (value > 0) then
			if (self.Working == false) then
				reqCool = 800
				reqHeavy = 0
				reqEnergy = 0
				if (self.val1 >= 800) then
					self.CurProj = "orack"
					RD_ConsumeResource(self, "coolant", 800)
					self.NTog = CurTime() + 8
					self.Working = true
					self.PIni = true
					self.PWHType = 0
					self.PWarhead = nil
					self.NWR = 85
					self.NWG = 26
					self.NWB = 255
					self.NWA = 255
				end
			end
		end
		
	
	end
end

function ENT:OnRemove()
	Dev_Unlink_All(self.Entity)
	if ( self.Active == true ) then
		self.Entity:StopSound( "apc_engine_start" )
		self.Entity:EmitSound( "apc_engine_stop" )
		self.Active = false
	end
end

function ENT:Think()
	self.val1 = RD_GetResourceAmount(self.Entity, "coolant")
	self.val2 = RD_GetResourceAmount(self.Entity, "heavy water")
	self.val3 = RD_GetResourceAmount(self.Entity, "energy")
	
	self:SetOverlayText( "Coolant: " .. self.val1 .. "\nHeavy Water: " .. self.val2 .. "\nEnergy: " .. self.val3 )

	if (self.Working == true) then
		wireoutWorking = 1
	end

	if (self.Working == true && self.val3 >= 300) then
		RD_ConsumeResource(self, "energy", 300)
	end

	if (self.Working == true && self.val3 < 300) then
		self.Entity:EmitSound( "common/warning.wav" )
		self.Working = false
	end

	if (self.Working == false) then
		wireoutWorking = 0
		self.Entity:StopSound( "common/warning.wav" )
		self.Entity:StopSound( "apc_engine_start" )
	end

	if (CurTime() >= self.NTog && self.Working == true) then
		local NWeap = ents.Create( self.CurProj )
		if ( !NWeap:IsValid() ) then return end
		NWeap:SetPos( self.Entity:GetPos() + (self.Entity:GetForward() * 140) )
		NWeap:SetAngles( self.Entity:GetAngles() )
		NWeap.Fired = false
		NWeap:Spawn()
		if (self.PIni == true) then
			NWeap:Initialize()
		end
		if (self.PWHType >= 0) then
			NWeap.WHType = self.PWHType
			if (self.PWHType == 2) then
				NWeap.WHMk2 = true
			end
		end
		if (self.PWarhead != nil) then
			NWeap.Warhead = self.PWarhead
			NWeap.OverlayText = "Warhead: " .. NWeap.Warhead
		end
		if (self.PWarhead == "plasbomb_head") then
			self.Entity:EmitSound( "Other/missilespawn.wav" )
		end
		NWeap:Activate()
		NWeap:SetColor( self.NWR, self.NWG, self.NWB, self.NWA)
		self.Working = false
		self.Entity:EmitSound( "ambient/machines/thumper_startup1.wav" )
	end

	buildtime = self.NTog - CurTime()

	if ( buildtime < 0) then
		truetime = 0
	end
	if ( buildtime >= 0) then
		truetime = buildtime
	end

	Wire_TriggerOutput(self.Entity, "Working", wireoutWorking)
        Wire_TriggerOutput(self.Entity, "BuildTime", truetime)
        Wire_TriggerOutput(self.Entity, "ReqCoolant", reqCool)
        Wire_TriggerOutput(self.Entity, "ReqHeavyWater", reqHeavy)
        Wire_TriggerOutput(self.Entity, "ReqEnergy", reqEnergy)
	
end

function ENT:PreEntityCopy()
	RD_BuildDupeInfo(self.Entity)
end

function ENT:PostEntityPaste( Player, Ent, CreatedEntities )
	RD_ApplyDupeInfo(Ent, CreatedEntities)
end